package com.hkd.user_service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkd.common.api.ApiResponse;
import com.hkd.common.api.SysException;
import com.hkd.user_service.api.*;
import com.hkd.user_service.api.entity.*;
import com.hkd.user_service.api.vo.BaseUserInfo;
import com.hkd.user_service.mapper.SysUserMapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HKD
 * @date 2022/8/20 19:11
 */
@RestController
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResponse initSysUser() {
        int count = this.count();
        if (count>0){
            throw new SysException("系统已存在用户");
        }
        SysUser user = new SysUser();
        user.setAccount("admin");
        user.setPassword("admin123");
        user.setName("系统");
        baseMapper.insert(user);
        SysRole role = new SysRole();
        role.setRoleName("超级管理员");
        roleService.insertRole(role);
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(role.getId());
        userRoleService.insertUserRole(userRole);
        List<SysMenu> menuList = menuService.initMenu().getData();
        //绑定所有角色
        List<SysRoleMenu> roleMenus=new ArrayList<>();
        for (SysMenu menu : menuList) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menu.getId());
            roleMenus.add(roleMenu);
        }
        roleMenuService.insertAll(roleMenus);
        return ApiResponse.ok();
    }

    @Override
    public ApiResponse<BaseUserInfo> getUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(SysUser::getAccount,account);
        SysUser user = this.getOne(userWrapper);
        BaseUserInfo baseUser = userToBaseUser(user);
        return ApiResponse.ok(baseUser);
    }

    @Override
    public ApiResponse<BaseUserInfo> getUserById(String userId) {
        SysUser user = this.getById(userId);
        BaseUserInfo baseUser = userToBaseUser(user);
        return ApiResponse.ok(baseUser);
    }

    private BaseUserInfo userToBaseUser(SysUser user){
        BaseUserInfo baseUser = new BaseUserInfo();
        BeanUtils.copyProperties(user,baseUser);
        List<String> authPath = roleService.getAuthPathByUserId(user.getId()).getData();
        List<String> roleNames = roleService.getRoleByUserId(user.getId()).getData();
        baseUser.setRoleNames(roleNames);
        baseUser.setAuthPath(authPath);
        return baseUser;
    }
}
